home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
-
- public class Chart extends Applet {
- static final int VERTICAL = 0;
- static final int HORIZONTAL = 1;
- static final int SOLID = 0;
- static final int STRIPED = 1;
- int orientation;
- String title;
- Font titleFont;
- FontMetrics titleFontMetrics;
- int titleHeight = 15;
- int columns;
- int[] values;
- Object[] colors;
- Object[] labels;
- int[] styles;
- int scale = 10;
- int maxLabelWidth;
- int barWidth;
- int barSpacing = 10;
- int max;
-
- public synchronized void init() {
- this.titleFont = new Font("Courier", 1, 12);
- this.titleFontMetrics = ((Component)this).getFontMetrics(this.titleFont);
- this.title = ((Applet)this).getParameter("title");
- if (this.title == null) {
- this.title = "Chart";
- }
-
- String rs = ((Applet)this).getParameter("columns");
- if (rs == null) {
- this.columns = 5;
- } else {
- this.columns = Integer.parseInt(rs);
- }
-
- rs = ((Applet)this).getParameter("scale");
- if (rs == null) {
- this.scale = 10;
- } else {
- this.scale = Integer.parseInt(rs);
- }
-
- rs = ((Applet)this).getParameter("orientation");
- if (rs == null) {
- this.orientation = 0;
- } else if (rs.toLowerCase().equals("vertical")) {
- this.orientation = 0;
- } else if (rs.toLowerCase().equals("horizontal")) {
- this.orientation = 1;
- } else {
- this.orientation = 0;
- }
-
- this.values = new int[this.columns];
- this.colors = new Color[this.columns];
- this.labels = new String[this.columns];
- this.styles = new int[this.columns];
-
- for(int i = 0; i < this.columns; ++i) {
- rs = ((Applet)this).getParameter("C" + (i + 1));
- if (rs != null) {
- try {
- this.values[i] = Integer.parseInt(rs);
- } catch (NumberFormatException var3) {
- this.values[i] = 0;
- }
- }
-
- if (this.values[i] > this.max) {
- this.max = this.values[i];
- }
-
- rs = ((Applet)this).getParameter("C" + (i + 1) + "_label");
- this.labels[i] = rs == null ? "" : rs;
- this.maxLabelWidth = Math.max(this.titleFontMetrics.stringWidth((String)this.labels[i]), this.maxLabelWidth);
- rs = ((Applet)this).getParameter("C" + (i + 1) + "_style");
- if (rs != null && !rs.toLowerCase().equals("solid")) {
- if (rs.toLowerCase().equals("striped")) {
- this.styles[i] = 1;
- } else {
- this.styles[i] = 0;
- }
- } else {
- this.styles[i] = 0;
- }
-
- rs = ((Applet)this).getParameter("C" + (i + 1) + "_color");
- if (rs != null) {
- if (rs.equals("red")) {
- this.colors[i] = Color.red;
- } else if (rs.equals("green")) {
- this.colors[i] = Color.green;
- } else if (rs.equals("blue")) {
- this.colors[i] = Color.blue;
- } else if (rs.equals("pink")) {
- this.colors[i] = Color.pink;
- } else if (rs.equals("orange")) {
- this.colors[i] = Color.orange;
- } else if (rs.equals("magenta")) {
- this.colors[i] = Color.magenta;
- } else if (rs.equals("cyan")) {
- this.colors[i] = Color.cyan;
- } else if (rs.equals("white")) {
- this.colors[i] = Color.white;
- } else if (rs.equals("yellow")) {
- this.colors[i] = Color.yellow;
- } else if (rs.equals("gray")) {
- this.colors[i] = Color.gray;
- } else if (rs.equals("darkGray")) {
- this.colors[i] = Color.darkGray;
- } else {
- this.colors[i] = Color.gray;
- }
- } else {
- this.colors[i] = Color.gray;
- }
- }
-
- switch (this.orientation) {
- case 0:
- default:
- this.barWidth = this.maxLabelWidth;
- ((Component)this).resize(Math.max(this.columns * (this.barWidth + this.barSpacing), this.titleFontMetrics.stringWidth(this.title)) + this.titleFont.getSize() + 5, this.max * this.scale + 2 * this.titleFont.getSize() + 5 + this.titleFont.getSize());
- return;
- case 1:
- this.barWidth = this.titleFont.getSize();
- ((Component)this).resize(Math.max(this.max * this.scale + this.titleFontMetrics.stringWidth("" + this.max), this.titleFontMetrics.stringWidth(this.title)) + this.maxLabelWidth + 5, this.columns * (this.barWidth + this.barSpacing) + this.titleFont.getSize() + 10);
- }
- }
-
- public synchronized void paint(Graphics g) {
- g.setColor(Color.black);
- int i = this.titleFontMetrics.stringWidth(this.title);
- g.setFont(this.titleFont);
- g.drawString(this.title, Math.max((((Component)this).size().width - i) / 2, 0), ((Component)this).size().height - this.titleFontMetrics.getDescent());
-
- for(int var8 = 0; var8 < this.columns; ++var8) {
- switch (this.orientation) {
- case 0:
- default:
- int cx = Math.max(this.barWidth + this.barSpacing, this.maxLabelWidth) * var8 + this.barSpacing;
- int var10002 = this.columns * (this.barWidth + 2 * this.barSpacing);
- cx += Math.max((((Component)this).size().width - var10002) / 2, 0);
- int var10001 = this.values[var8];
- int cy = ((Component)this).size().height - var10001 * this.scale - 1 - 2 * this.titleFont.getSize();
- g.setColor(Color.black);
- g.drawString((String)this.labels[var8], cx, ((Component)this).size().height - this.titleFont.getSize() - this.titleFontMetrics.getDescent());
- if (this.colors[var8] == Color.black) {
- g.setColor(Color.gray);
- }
-
- g.fillRect(cx + 5, cy - 3, this.barWidth, this.values[var8] * this.scale);
- g.setColor((Color)this.colors[var8]);
- switch (this.styles[var8]) {
- case 0:
- default:
- g.fillRect(cx, cy, this.barWidth, this.values[var8] * this.scale);
- break;
- case 1:
- int steps = this.values[var8] * this.scale / 2;
-
- for(int j = 0; j < steps; ++j) {
- int ys = cy + 2 * j;
- g.drawLine(cx, ys, cx + this.barWidth, ys);
- }
- }
-
- g.drawString("" + this.values[var8], cx, cy - this.titleFontMetrics.getDescent());
- break;
- case 1:
- int cy = (this.barWidth + this.barSpacing) * var8 + this.barSpacing;
- int cx = this.maxLabelWidth + 1;
- cx += Math.max((((Component)this).size().width - (this.maxLabelWidth + 1 + this.titleFontMetrics.stringWidth("" + this.max) + this.max * this.scale)) / 2, 0);
- g.setColor(Color.black);
- g.drawString((String)this.labels[var8], cx - this.maxLabelWidth - 1, cy + this.titleFontMetrics.getAscent());
- if (this.colors[var8] == Color.black) {
- g.setColor(Color.gray);
- }
-
- g.fillRect(cx + 3, cy + 5, this.values[var8] * this.scale, this.barWidth);
- g.setColor((Color)this.colors[var8]);
- switch (this.styles[var8]) {
- case 0:
- default:
- g.fillRect(cx, cy, this.values[var8] * this.scale, this.barWidth);
- break;
- case 1:
- int steps = this.values[var8] * this.scale / 2;
-
- for(int j = 0; j < steps; ++j) {
- int ys = cx + 2 * j;
- g.drawLine(ys, cy, ys, cy + this.barWidth);
- }
- }
-
- g.drawString("" + this.values[var8], cx + this.values[var8] * this.scale + 3, cy + this.titleFontMetrics.getAscent());
- }
- }
-
- }
- }
-